package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.User;
import com.battle.pojo.entity.UserCooperation;
import com.battle.pojo.entity.WarCaseStudy;
import com.battle.pojo.enums.UserCooperationStatusEnum;
import com.battle.pojo.enums.UserShareTypeEnum;
import com.battle.pojo.param.userCooperation.*;
import com.battle.pojo.vo.userCooperation.*;
import com.battle.pojo.dto.userCooperation.*;
import com.battle.service.repository.in.UserCooperationRepository;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.repository.in.WarCaseStudyRepository;
import com.battle.service.service.in.UserCooperationService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 【描 述】：用户协作服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class UserCooperationServiceImpl implements UserCooperationService {

    private final UserCooperationRepository userCooperationRepository;
    private final WarCaseStudyRepository studyRepository;
    private final UserRepository userRepository;

    public UserCooperationServiceImpl(UserCooperationRepository userCooperationRepository, WarCaseStudyRepository studyRepository, UserRepository userRepository) {
        this.userCooperationRepository = userCooperationRepository;
        this.studyRepository = studyRepository;
        this.userRepository = userRepository;
    }


    /**
     * 创建用户协作
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createUserCooperation(CreateUserCooperationParam param) {
        List<UserCooperation> userCooperationList=new ArrayList<>();

        for (Long userId : param.getUserId()) {
            UserCooperation userCooperation = new UserCooperation();
            BeanUtils.copyProperties(param, userCooperation);
            Long id = IdGenerator.SNOW.generate();
            userCooperation.setId(id);
            userCooperation.setUserId(userId);
            userCooperation.setStatus(UserCooperationStatusEnum.wait);
            userCooperationList.add(userCooperation);
        }
        userCooperationRepository.saveBatch(userCooperationList);
    }

    /**
     * 修改用户协作
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editUserCooperation(EditUserCooperationParam param) {
        Long id = param.getId();
        UserCooperation userCooperation = userCooperationRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, userCooperation);
        userCooperation.setStatus(UserCooperationStatusEnum.pass);
        userCooperationRepository.edit(userCooperation);

        if (userCooperation.getType().getId()== UserShareTypeEnum.examplesOfBattles.getId()){
            WarCaseStudy study = studyRepository.getByIdCheck(userCooperation.getFid());
            study.setResult(param.getResult());
            study.setResultPicture(param.getResultPicture());
            studyRepository.updateById(study);
        }

    }

    /**
     * 移除用户协作
     *
     * @param id
     */
    @Override
    public void removeUserCooperation(Long id) {
        userCooperationRepository.removeById(id);
    }

    /**
     * 启动/禁用用户协作
     *
     * @param id
     */
    @Override
    public void enabledUserCooperation(Long id) {
        UserCooperation userCooperation = userCooperationRepository.getByIdCheck(id);
        userCooperation.setIsDisable(!userCooperation.getIsDisable());
        userCooperationRepository.edit(userCooperation);
    }

    /**
     * 获取用户协作详情
     *
     * @param id
     * @return
     */
    @Override
    public GetUserCooperationVo getUserCooperation(Long id) {
        UserCooperation userCooperation = userCooperationRepository.getByIdCheck(id);
        WarCaseStudy study = studyRepository.getByIdCheck(userCooperation.getFid());
        GetUserCooperationVo vo = new GetUserCooperationVo();
        BeanUtils.copyProperties(study, vo);
        return vo;
    }

    /**
     * 获取用户协作 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetUserCooperationListVo> getUserCooperationList(GetUserCooperationListParam param) {

        SelectUserCooperationDto dto = new SelectUserCooperationDto();
        BeanUtils.copyProperties(param, dto);
        Page<GetUserCooperationListVo> page = userCooperationRepository.selectUserCooperation(dto);
        List<GetUserCooperationListVo> list = page.getRecords();

        if (CollectionUtil.isNotEmpty(list)) {
            List<Long> userIds = new ArrayList<>();

            list.forEach(v -> {
                userIds.add(v.getInitiatorId());
                userIds.add(v.getUserId());
            });

            Map<Long, User> userMap = userRepository.getMap(userIds);

            for (GetUserCooperationListVo vo : list) {
                User initiator = userMap.get(vo.getInitiatorId());
                if (initiator != null) {
                    vo.setInitiatorName(initiator.getName());
                }
                User user = userMap.get(vo.getUserId());
                if (user != null) {
                    vo.setUserName(user.getName());
                }
                if (vo.getStatus().getId()!=UserCooperationStatusEnum.pass.getId()){
                    vo.setUpdateTime(null);
                }
            }
        }

        return new PageResult<>(list, page);

    }

    /**
     * 获取用户协作下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetUserCooperationDownListVo> getUserCooperationDownList(GetUserCooperationDownListParam param) {
        UserCooperation po = new UserCooperation();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<UserCooperation> userCooperationList = userCooperationRepository.getListByEntity(po);

        List<GetUserCooperationDownListVo> list = new ArrayList<>();
        for (UserCooperation userCooperation : userCooperationList) {
            GetUserCooperationDownListVo vo = new GetUserCooperationDownListVo();
            BeanUtils.copyProperties(userCooperation, vo);
            list.add(vo);
        }
        return list;
    }


}



